
#ifndef _COMM_STRUCT_H
#define _COMM_STRUCT_H

#include "stdint.h"
#include "arm_math.h"


#define FRAME_HEAD_H  0xF0
#define FRAME_HEAD_L  0xF0


// Navigation send mesg frame to MCU --------
#define ID_CMD_TIME_SET        0x01
#define ID_CMD_SPEED_SET       0x02
#define ID_CMD_LED_SET         0x03
#define ID_CMD_CHARGE_SET      0x04
#define ID_CMD_CLEAN_SET       0x05
#define ID_CMD_MOTOR_SET       0x06
#define ID_CMD_HEARTBEAT       0x07
#define ID_CMD_LIGHTMODE_SET   0x08
#define ID_CMD_GS2_PARAM_GET   0x09
#define ID_CMD_BRUSH_SET       0x0A

#define ID_CMD_SHIELD_CLIFF    0x0C
#define ID_CMD_SHIELD_CLIFF_A  0x0D

#define DATA_LEGTH_TIME_SET      8
#define DATA_LEGTH_SPEED_SET     8
#define DATA_LEGTH_LED_SET       1
#define DATA_LEGTH_CHARGE_SET    1
#define DATA_LEGTH_CLEAN_SET     1
#define DATA_LEGTH_MOTOR_SET     1
#define DATA_LEGTH_HEARTBEAT     0
#define DATA_LEGTH_LIGHTMODE_SET 1
#define DATA_LEGTH_BRUSH_SET     6

#define DATA_LEGTH_SHIELD_CLIFF  1

// MCU send mesg frame to Navigation --------
#define ID_SPEED_UPLOAD        0x11
#define ID_ENCODER_UPLOAD      0x12
#define ID_IMU_RAW_UPLOAD      0x13
#define ID_LIGHTFLOW_UPLOAD    0x14
#define ID_WALL_UPLOAD         0x15
#define ID_CHARGE_POS_UPLOAD   0x16
#define ID_CLIFF_UPLOAD        0x17
#define ID_BUMPER_UPLOAD       0x18
#define ID_KEY_STATUS_UPLOAD   0x19
#define ID_DROP_STATUS_UPLOAD  0x1A
#define ID_CURRENT_UPLOAD      0x1B
#define ID_EVENT_UPLOAD        0x1C
#define ID_BMS_MANAGE_UPLOAD   0x1D
#define ID_GS2_DATA_UPLOAD     0x1F
#define ID_MATERIAL_UPLOAD     0x20

#define DATA_LEGTH_SPEED       10
#define DATA_LEGTH_ENCODER     6
#define DATA_LEGTH_IMU_RAW     26
#define DATA_LEGTH_LIGHTFLOW   6+3
#define DATA_LEGTH_WALL        6
#define DATA_LEGTH_CHARGE_POS  6
#define DATA_LEGTH_CLIFF       3
#define DATA_LEGTH_BUMPER      3
#define DATA_LEGTH_KEY         3
#define DATA_LEGTH_DROP_STATUS 3
#define DATA_LEGTH_CURRENT     26
#define DATA_LEGTH_EVENT       5+1
#define DATA_LEGTH_BMS         4

#define DATA_LEGTH_GS2_DATA    331
#define DATA_LEGTH_GS2_PARAM   18

#define DATA_LEGTH_MATERIAL    6

// Universal uart mesg frame struct ---------
typedef struct
{
	uint8_t  head_H;
	uint8_t  head_L;
	uint8_t  frame_ID_H;
	uint8_t  frame_ID_L;
	
	uint8_t  length;
	uint8_t  cmd_ID;
	
	uint8_t* databuf;
	
	uint8_t  CRC_H;
	uint8_t  CRC_L;

}STR_COMM_FRAME;


// -----------------------------------------

typedef union
{
	struct
	{
		uint64_t time;
	}real;
	
	uint8_t data[8];

}STR_COMM_TIME_SET;


typedef union
{
	struct
	{
		uint8_t state;
	
	}real;

	uint8_t data;

}STR_COMM_LED_SET;


typedef union
{
	struct
	{
		uint8_t mainBrush      :1;
		uint8_t leftBrush      :1;
		uint8_t rightBrush     :1;
		uint8_t dustsuctionFan :1;
		
		uint8_t reserve        :4;
	
	}real;
	
	uint8_t data;

}STR_COMM_MOTOR_SET;



typedef union
{
	struct
	{
		uint8_t leftBrush_speed;
		uint8_t rightBrush_speed;
		uint8_t rollBrush_speed;
		uint8_t dustfan_speed;
		uint8_t leftMop_speed;
		uint8_t rightMop_speed;
	
	}real;
	
	uint8_t data[6];

}STR_COMM_BRUSH_SET;





typedef union
{
	struct
	{
		float32_t speedLinear;
		float32_t speedAngle;
		
	}real;
	
	uint8_t data[8];

}STR_COMM_SPEED_SET;


typedef union
{
	struct
	{
		uint8_t reserve;
	}real;
	
	uint8_t data;

}STR_COMM_CHARGE_SET;


typedef union
{
	struct
	{
		uint8_t clean_mode   :4;
		uint8_t mop_mode     :4;
	}real;
	
	uint8_t data;

}STR_COMM_CLEAN_SET;


typedef union
{

	struct
	{
		uint8_t reserve;
	}real;
	
	uint8_t data;

}STR_COMM_HEARTBEAT;


typedef union
{

	struct
	{
		uint8_t mode;
	}real;
	
	uint8_t data;

}STR_COMM_LIGHTMODE;


typedef union
{
	struct
	{
		uint8_t en_shield;
		
	}real;
	
	uint8_t data;

}STR_COMM_SHIELD_CLIFF;


// ----------------------------------------------------------

// upload linear speed and angle speed
typedef union
{
	struct
	{
		uint16_t  reserve;
		uint16_t  time;
		float32_t speedwheel_L;
	    float32_t speedwheel_R;
		
	}real;
	
	uint8_t data[12];

}STR_UPLOAD_SPEED;


// upload left and right motor encoder CNT 
typedef union
{
	struct
	{
		uint16_t reserve;
		uint16_t time;
		uint16_t  m1_value;
		uint16_t  m2_value;
	
	}real;
	
	uint8_t data[8];

}STR_UPLOAD_ENCODER;


// upload IMU raw data accel_xyz and gyro_xyz
typedef union
{
	struct
	{
		uint16_t  reserve;
		uint16_t  time;
		float32_t accel[3];
		float32_t gyro[3];

	}real;
	
	uint8_t data[28];

}STR_UPLOAD_IMU;


// upload lightflow sensor data
typedef union
{
	struct
	{
		uint16_t time;
		int16_t  flowX;
		int16_t  flowY;
		uint16_t quality;
		uint8_t  mode;
	
	}real;
	
	uint8_t data[6+3];

}STR_UPLOAD_LIGHTFLOW;


// upload wall sensor data
typedef union
{
	struct
	{
		uint16_t time;
		uint16_t distance[2];
	
	}real;

	uint8_t data[6];

}STR_UPLOAD_WALL;
















typedef union
{
	struct
	{
		uint16_t time;
		uint8_t  chargeSignal[4];
	
	}real;

	uint8_t data[6];

}STR_UPLOAD_CHARGE;


typedef union
{
	struct 
	{
		uint16_t time;
		
		uint8_t  sta_left        :1;
		uint8_t  sta_right       :1;
		uint8_t  sta_front_left  :1;		
		uint8_t  sta_front_right :1;
		
		uint8_t  sta_back_left   :1;
		uint8_t  sta_back_right  :1;
		uint8_t  reserve         :2;		
	
	}real;
	
	uint8_t data[3];
	
}STR_UPLOAD_CLIFF;


typedef union
{
	struct
	{
		uint16_t time;  
		uint8_t  sta_left       :1;
		uint8_t  sta_right      :1;
		uint8_t  sta_left_right :1;
		
		uint8_t   reserve       :5;
	
	}real;
	
	uint8_t data[3];

}STR_UPLOAD_BUMPER;


typedef union
{
	struct
	{
		uint16_t time;
		uint8_t  event_x1    :1;
		uint8_t  event_x2    :1;
		uint8_t  event_x3    :1;
		uint8_t  event_x4    :1;
		uint8_t  event_x5    :1;
		uint8_t  event_x6    :1;
		
		uint8_t  reserve     :2;
	
	}real;
	
	uint8_t data[3];

}STR_UPLOAD_KEY;


typedef union
{
	
	struct 
	{
		uint16_t time;
		uint8_t  sta_left   :1;
		uint8_t  sta_right  :1;
		
		uint8_t  reserve    :6;
	
	}real;

	uint8_t data[3];


}STR_UPLOAD_DROP;

typedef union
{
	struct
	{
		uint16_t reserve;
		uint16_t time;
		
		float32_t mainBrush;
		float32_t leftBrush;
		float32_t rightBrush;
		float32_t dustSuctionFan;
		float32_t wheelleft;
		float32_t wheelright;
	
	}real;
	
	uint8_t data[28];


}STR_UPLOAD_CURRENT;


typedef union
{
	struct
	{
		uint16_t time;
		uint16_t brake_distance;
		
		uint8_t  brake_Status  :1;
		uint8_t  bumper_L      :1;
		uint8_t  bumper_R      :1;
		uint8_t  cliff_1       :1;
		uint8_t  cliff_2       :1;
		uint8_t  cliff_3       :1;
		uint8_t  cliff_4       :1;
		uint8_t  cliff_5       :1;
		
		uint8_t  cliff_6       :1;
		uint8_t  reserve       :7;
	
	}real;
	
	uint8_t data[6];

}STR_UPLOAD_EVENT;


typedef union
{
	struct
	{	
		uint16_t time;
		uint8_t  status;
		uint8_t  level;
	
	}real;
	
	uint8_t data[4];

}STR_UPLOAD_BMS_MANAGE;


typedef union
{
	struct
	{
		uint16_t time;
		uint16_t sort;
		uint16_t distance;
	}real;
	
	uint8_t data[6];

}STR_UPLOAD_MATERIAL;



#endif

